home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / Bounce / PalAnim.c < prev   
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.3 KB  |  161 lines

  1. /*----------------------------------------------
  2.    PALANIM.C -- Palette Animation Shell Program
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. extern HPALETTE CreateRoutine  (HWND) ;
  9. extern void     PaintRoutine   (HDC, int, int) ;
  10. extern void     TimerRoutine   (HDC, HPALETTE) ;
  11. extern void     DestroyRoutine (HWND, HPALETTE) ;
  12.  
  13. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  14.  
  15. extern TCHAR szAppName [] ;
  16. extern TCHAR szTitle [] ;
  17.  
  18. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  19.                     PSTR szCmdLine, int iCmdShow)
  20. {
  21.      HWND     hwnd ;
  22.      MSG      msg ;
  23.      WNDCLASS wndclass ;
  24.  
  25.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  26.      wndclass.lpfnWndProc   = WndProc ;
  27.      wndclass.cbClsExtra    = 0 ;
  28.      wndclass.cbWndExtra    = 0 ;
  29.      wndclass.hInstance     = hInstance ;
  30.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  31.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  32.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  33.      wndclass.lpszMenuName  = NULL ;
  34.      wndclass.lpszClassName = szAppName ;
  35.      
  36.      if (!RegisterClass (&wndclass))
  37.      {
  38.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  39.                       szAppName, MB_ICONERROR) ;
  40.           return 0 ;
  41.      }
  42.      
  43.      hwnd = CreateWindow (szAppName, szTitle, 
  44.                           WS_OVERLAPPEDWINDOW, 
  45.                           CW_USEDEFAULT, CW_USEDEFAULT,
  46.                           CW_USEDEFAULT, CW_USEDEFAULT,
  47.                           NULL, NULL, hInstance, NULL) ;
  48.  
  49.      if (!hwnd)
  50.           return 0 ;
  51.  
  52.      ShowWindow (hwnd, iCmdShow) ;
  53.      UpdateWindow (hwnd) ;
  54.  
  55.      while (GetMessage (&msg, NULL, 0, 0))
  56.      {
  57.           TranslateMessage (&msg) ;
  58.           DispatchMessage (&msg) ;
  59.      }
  60.      return msg.wParam ;
  61. }
  62.  
  63. BOOL CheckDisplay (HWND hwnd)
  64. {
  65.      HDC hdc ;
  66.      int iPalSize ;
  67.  
  68.      hdc = GetDC (hwnd) ;
  69.      iPalSize = GetDeviceCaps (hdc, SIZEPALETTE) ;
  70.      ReleaseDC (hwnd, hdc) ;
  71.  
  72.      if (iPalSize != 256)
  73.      {
  74.           MessageBox (hwnd, TEXT ("This program requires that the video ")
  75.                             TEXT ("display mode have a 256-color palette."),
  76.                       szAppName, MB_ICONERROR) ;
  77.           return FALSE ;
  78.      }
  79.      return TRUE ;
  80. }
  81.  
  82. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  83. {
  84.      static HPALETTE hPalette ;
  85.      static int      cxClient, cyClient ;
  86.      HDC             hdc ;
  87.      PAINTSTRUCT     ps ;
  88.  
  89.      switch (message)
  90.      {
  91.      case WM_CREATE:
  92.           if (!CheckDisplay (hwnd))
  93.                return -1 ;
  94.  
  95.           hPalette = CreateRoutine (hwnd) ;
  96.           return 0 ;
  97.      
  98.      case WM_DISPLAYCHANGE:
  99.           if (!CheckDisplay (hwnd))
  100.                DestroyWindow (hwnd) ;
  101.  
  102.           return 0 ;
  103.  
  104.      case WM_SIZE:
  105.           cxClient = LOWORD (lParam) ;
  106.           cyClient = HIWORD (lParam) ;
  107.           return 0 ;
  108.  
  109.      case WM_PAINT:
  110.           hdc = BeginPaint (hwnd, &ps) ;
  111.  
  112.           SelectPalette (hdc, hPalette, FALSE) ;
  113.           RealizePalette (hdc) ;
  114.  
  115.           PaintRoutine (hdc, cxClient, cyClient) ;
  116.  
  117.           EndPaint (hwnd, &ps) ;
  118.           return 0 ;
  119.  
  120.      case WM_TIMER:
  121.           hdc = GetDC (hwnd) ;
  122.  
  123.           SelectPalette (hdc, hPalette, FALSE) ;
  124.  
  125.           TimerRoutine (hdc, hPalette) ;
  126.  
  127.           ReleaseDC (hwnd, hdc) ;
  128.           return 0 ;
  129.  
  130.      case WM_QUERYNEWPALETTE:
  131.           if (!hPalette)
  132.                return FALSE ;
  133.  
  134.           hdc = GetDC (hwnd) ;
  135.           SelectPalette (hdc, hPalette, FALSE) ;
  136.           RealizePalette (hdc) ;
  137.           InvalidateRect (hwnd, NULL, TRUE) ;
  138.  
  139.           ReleaseDC (hwnd, hdc) ;
  140.           return TRUE ;
  141.  
  142.      case WM_PALETTECHANGED:
  143.           if (!hPalette || (HWND) wParam == hwnd)
  144.                break ;
  145.  
  146.           hdc = GetDC (hwnd) ;
  147.           SelectPalette (hdc, hPalette, FALSE) ;
  148.           RealizePalette (hdc) ;
  149.           UpdateColors (hdc) ;
  150.  
  151.           ReleaseDC (hwnd, hdc) ;
  152.           break ;
  153.  
  154.      case WM_DESTROY:
  155.           DestroyRoutine (hwnd, hPalette) ;
  156.           PostQuitMessage (0) ;
  157.           return 0 ;
  158.      }
  159.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  160. }
  161.